package com.tech.arno.spicysdk.videosdk.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.tech.arno.spicysdk.R;
import com.tech.arno.spicysdk.videosdk.activity.AdBrowserActivity;
import com.tech.arno.spicysdk.videosdk.constant.SDKConstant;
import com.tech.arno.spicysdk.videosdk.core.slot.VideoSDKSlot;
import com.tech.arno.spicysdk.videosdk.module.AdValue;
import com.tech.arno.spicysdk.videosdk.report.ReportManager;
import com.util.LogUtil;

/**
 * @author: qndroid
 * @function: 全屏显示视频 复用CustomVideoView
 * @date: 16/6/7
 */
public class VideoFullDialog extends Dialog
        implements CustomVideoView.ADVideoPlayerListener {

    private static final String TAG = VideoFullDialog.class.getSimpleName();
    private CustomVideoView mVideoView;

    private Context mContext;
    private RelativeLayout mRootView;
    private ViewGroup mParentView;
    private ImageView mBackButton;

    private AdValue mXAdInstance;
    private int mPosition;//从小屏到全屏时的播放位置
    private FullToSmallListener mListener;
    //    private XADFrameImageLoadListener mFrameLoadListener;
    private boolean isFirst = true;//防止出现bug

    private VideoSDKSlot.VideoSDKSlotListener mSlotListener;

    public VideoFullDialog(Context context, CustomVideoView mraidView, AdValue instance,
                           int position) {
        super(context, R.style.dialog_full_screen);
        this.mContext = context;
        this.mXAdInstance = instance;
        this.mPosition = position;
        this.mVideoView = mraidView;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.xadsdk_dialog_video_layout);
        initVideoView();
    }

    /**
     * @param listener FullToSmallListener
     * @function 注入事件监听回调
     * @author ArnoFrost
     * @time 2017/12/14 0014  14:13
     */
    public void setListener(FullToSmallListener listener) {
        this.mListener = listener;
    }

//    public void setFrameLoadListener(XADFrameImageLoadListener listener) {
//        this.mFrameLoadListener = listener;
//    }

    public void setSlotListener(VideoSDKSlot.VideoSDKSlotListener slotListener) {
        this.mSlotListener = slotListener;
    }

    private void initVideoView() {
        mParentView = (RelativeLayout) findViewById(R.id.content_layout);
        mBackButton = (ImageView) findViewById(R.id.xadsdk_player_close_btn);
        mBackButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickBackBtn();
            }
        });
        mRootView = (RelativeLayout) findViewById(R.id.root_view);
        mRootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickVideo();
            }
        });
        mVideoView.setListener(this);
        mVideoView.mute(false);//全屏不静音
        mParentView.addView(mVideoView);
    }

    /**
     * @name onWindowFocusChanged
     * @function 焦点状态改变回调
     * @author ArnoFrost
     * @time 2017/12/14 0014  14:25
     */
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        LogUtil.i(TAG, "onWindowFocusChanged");
        mVideoView.isShowFullBtn(false); //防止第一次，有些手机仍显示全屏按钮
        if (!hasFocus) {
            //未获取到焦点
            mPosition = mVideoView.getCurrentPosition();
            mVideoView.pauseForFullScreen();
        } else {
            if (isFirst) { //为了适配某些手机不执行seekandresume中的播放方法
                mVideoView.seekAndResume(mPosition);
            } else {
                mVideoView.resume();
            }
        }
        isFirst = false;
    }

    /**
     * @name dismiss
     * @function 销毁时调用
     * @author ArnoFrost
     * @time 2017/12/14 0014  14:35
     */
    @Override
    public void dismiss() {
        LogUtil.e(TAG, "dismiss");
        //从ParentView中移除
        this.mParentView.removeView(mVideoView);
        super.dismiss();
    }

    /**
     * @name onBackPressed
     * @function back键点击回调监听
     * @author ArnoFrost
     * @time 2017/12/14 0014  14:20
     */
    @Override
    public void onBackPressed() {
        onClickBackBtn();
        super.onBackPressed();
    }

    @Override
    public void onClickFullScreenBtn() {
        onClickVideo();
    }

    @Override
    public void onClickVideo() {
        Log.d(TAG, "onClickVideo: Full");
        String desationUrl = mXAdInstance.clickUrl;
        if (mSlotListener != null) {
            if (mVideoView.isFrameHidden() && !TextUtils.isEmpty(desationUrl)) {
                mSlotListener.onClickVideo(desationUrl);
                try {
                    ReportManager.pauseVideoReport(mXAdInstance.clickMonitor, mVideoView.getCurrentPosition()
                            / SDKConstant.MILLION_UNIT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else {
            //走默认样式
            if (mVideoView.isFrameHidden() && !TextUtils.isEmpty(desationUrl)) {
                Intent intent = new Intent(mContext, AdBrowserActivity.class);
                intent.putExtra(AdBrowserActivity.KEY_URL, mXAdInstance.clickUrl);
                mContext.startActivity(intent);
                try {
                    ReportManager.pauseVideoReport(mXAdInstance.clickMonitor, mVideoView.getCurrentPosition()
                            / SDKConstant.MILLION_UNIT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * @name onClickBackBtn
     * @function 返回按钮点击回到小屏
     * @author ArnoFrost
     * @time 2017/12/14 0014  14:19
     */
    @Override
    public void onClickBackBtn() {
        this.dismiss();
        try {
            ReportManager.exitfullScreenReport(mXAdInstance.event.exitFull.content, mVideoView.getCurrentPosition()
                    / SDKConstant.MILLION_UNIT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (mListener != null) {
            mListener.getCurrentPlayPosition(this.mVideoView.getCurrentPosition());
        }
    }

    /***************************
     * 实现了ADVideoPlayerListener接口中方法
     * ***************************/
    @Override
    public void onAdVideoLoadSuccess() {
        if (mVideoView != null) {
            mVideoView.resume();
        }
    }

    @Override
    public void onAdVideoLoadFailed() {
    }

    /*
    * 与小屏播放处理不一样,单独处理
    * */
    @Override
    public void onAdVideoLoadComplete() {
        try {
            int position = mVideoView.getDuration() / SDKConstant.MILLION_UNIT;
            ReportManager.sueReport(mXAdInstance.endMonitor, true, position);
        } catch (Exception e) {
            e.printStackTrace();
        }

        dismiss();
        if (mListener != null) {
            mListener.playComplete();
        }
    }

    @Override
    public void onBufferUpdate(int time) {
        try {
            if (mXAdInstance != null) {
                ReportManager.suReport(mXAdInstance.middleMonitor, time / SDKConstant.MILLION_UNIT);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //点击视频复写
    @Override
    public void onClickPlay() {
        LogUtil.d(TAG,"onClickPlay");
        if (mVideoView != null) {
            if (mVideoView.isPlaying()) {
                mVideoView.pause();
            } else mVideoView.resume();
        }

    }

    /**
     * @author ArnoFrost
     * @name FullToSmallListener
     * @function 与业务逻辑层(VideoSlot)进行通信
     * @time 2017/12/14 0014  14:12
     */
    public interface FullToSmallListener {
        void getCurrentPlayPosition(int position);

        void playComplete();//全屏播放结束时回调
    }
}
